package com.itheima.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Student;
import com.itheima.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.xml.ws.Action;
import java.io.InputStream;
import java.util.List;

public class PageTest {

    private StudentMapper dao;
    private SqlSession sqlSession;

    @Before
    public void init()throws Exception{
        //加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        //获取工厂对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取dao代理对象
        dao=sqlSession.getMapper(StudentMapper.class);
    }


    @After
    public void destroy()throws Exception{
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }


    @Test
    public void findPage(){
        //设置分页参数
        PageHelper.startPage(1,2);
        //执行查询
        List<Student> all = dao.findAll();
        for (Student student : all) {
            System.out.println(student);
        }

        //将查询结果封装到PageInfo对象，后期传递到页面显示
        PageInfo<Student> info = new PageInfo<>(all);
        System.out.println(info.getPages());
        System.out.println(info.getTotal());

    }






}
